package kr.hyosang.android.penfo.parser;



public abstract class Parser<Result>
{
	public abstract Result parse(String data);
	
	public static int nStartIdx = 0;
	public static int nEndIdx = 0;
	
	public static String getSubContent(String body, String begin) {
		return getSubContent(body, begin, "", 0);
	}
    public static String getSubContent(String body, String begin, String end) {
        return getSubContent(body, begin, end, 0);
    }
    
    public static String getSubContent(String body, String begin, String end, int nSearchStartIdx) {
    	nStartIdx = nEndIdx = -1;
    	
    	if("".equals(begin)) {
    		nStartIdx = 0;
    	}else {
	        nStartIdx = body.indexOf(begin, nSearchStartIdx);
	        
	        if(nStartIdx == -1) {
	            return "";
	        }
	        
	        nStartIdx += begin.length();
    	}
        

        if("".equals(end)) {
            return body.substring(nStartIdx).trim();
        }else {
            nEndIdx = body.indexOf(end, nStartIdx);
            
            if(nEndIdx == -1) {
                return "";
            }
        
            return body.substring(nStartIdx, nEndIdx).trim();
        }
    }
    
    public static int getCommentFromSubject(String subject) {
        String cmt = "0";
        
        if(subject.endsWith("]")) {
            int idx = subject.lastIndexOf('[');
            if(idx > 0) {
                cmt = subject.substring(idx+1, subject.length()-1);
            }
        }
        
        try {
            return Integer.parseInt(cmt);
        }catch(NumberFormatException e) {
            return 0;
        }
    }
    
    public static String stripCommentFromSubject(String subject, int nComment) {
        if(nComment > 0) {
            return subject.substring(0, subject.length()-("["+nComment+"]").length()).trim();
        }else {
            return subject;
        }
    }
    
    public static boolean isLogin(String html) {
        //로그인되어있는지 체크한다.
        if(html.indexOf("function check_autologin()") > 0) {
            return false;
        }else {
            return true;
        }        
    }
    
    public static String getErrorMessage(String html) {
        String mesg = getSubContent(html, "<td align=center height=30 style=\"font-family:Tahoma;font-size:8pt;\">", "<center>");
        mesg = getSubContent(mesg, "<br>", "<br>");
        return mesg;
    }
    
    public static boolean isRedirectPage(String html) {
        if(html.indexOf("<meta http-equiv=\"refresh\" content=\"0;") >= 0) {
            return true;
        }else {
            return false;
        }
    }
}
